package ee.maoinvest.frontend.controller.command.substationcommands;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import ee.maoinvest.backend.model.data.Substation;
import ee.maoinvest.backend.services.SubstationService;
import ee.maoinvest.backend.services.implementations.SubstationServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;

public class GetAllSubstationsThatAreNotWrittenOffCommand implements Command
{
	private Logger logger = Logger.getLogger(GetAllSubstationsThatAreNotWrittenOffCommand.class);
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int PROBABLY_A_DATABASE_ERROR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		List<Substation> listOfSubstations = null;
		SubstationService substationService = null;
		
		try 
		{
			substationService = new SubstationServiceImpl();
			listOfSubstations = substationService.getAllSubstationsThatAreNotWrittenOff();
			request.setAttribute("listOfSubstations", listOfSubstations);
			operationResult = OPERATION_SUCCESSFUL;
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
			return PROBABLY_A_DATABASE_ERROR;
		}
		
		return operationResult;
	}
}

